package com.wenjiang.controller;


import com.aliyun.oss.OSS;
import com.wenjiang.common.annotation.RequireToken;
import com.wenjiang.common.annotation.TokenMallUser;
import com.wenjiang.common.annotation.TokenUserId;
import com.wenjiang.common.dto.LoginParam;
import com.wenjiang.common.dto.RegisterParam;
import com.wenjiang.common.dto.UpdatePasswordParam;
import com.wenjiang.common.dto.UserInfoParam;
import com.wenjiang.common.lang.Result;
import com.wenjiang.common.vo.AliLoginVo;
import com.wenjiang.entity.AliPayUser;
import com.wenjiang.entity.MallUser;
import com.wenjiang.service.MallUserService;
import com.wenjiang.service.impl.AiliLoginServiceImpl;
import com.wenjiang.util.OSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lwj
 * @since 2021-07-20
 */
@RequestMapping("/mallUser")
@RestController
@Slf4j
public class MallUserController {

    @Autowired
    MallUserService mallUserService;

    @Autowired
    AiliLoginServiceImpl ailiLongService;

    @Autowired
    OSS ossClient;

    @Autowired
    OSSUtils ossUtils;

    //添加用户
    @RequestMapping("/registerUser")
    public Object registerUser(@RequestBody RegisterParam registerParam){

        //添加用户
        Result result = mallUserService.registerUser(registerParam);

        return result;
    }


    //登陆用户
    @RequestMapping("/loginUser")
    public Object loginUser(@RequestBody LoginParam loginParam){
        //登陆账户
        Result result = mallUserService.loginUser(loginParam);
        return result;
    }



    //生成登陆网址

    @RequestMapping("/aliLogin")
    public String getAliLoginUrl() throws IOException {

        return (ailiLongService.createUrl());
    }



    //支付宝登陆成功后
    @RequestMapping("/aliLoginUser")
    public void aliLoginUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        log.info("支付宝登录成功!");
        //获取授权码 授权信息
        String authCode = request.getParameter("auth_code");
        String appID = request.getParameter("app_id");
        String scope = request.getParameter("scope");
        //获取accessToken
        if (StringUtils.isNotEmpty(authCode)) {
            //获取access_token
                String accessToken = ailiLongService.getAccessToken(authCode);
            //获取用户信息
            if (StringUtils.isNotEmpty(accessToken)) {
                //获取用户信息
                AliPayUser alipayUser = ailiLongService.getUserInfoByToken(accessToken);

                //构建参数
                System.out.println("获取的用户信息"+alipayUser);

                //往数据库插入数据
                AliLoginVo aliLoginVo = mallUserService.aliPayLoginUser(alipayUser);
                //重定向导到登陆页面
                response.sendRedirect("http://localhost:8080/#/login?token="+aliLoginVo.getToken());
            }

    }

    }





    //获取用户基本信息
    @RequestMapping("/getUserInfo")
    @RequireToken
    public  Object  getUserInfo(@TokenUserId Long userId){

        return mallUserService.getUserInfo(userId);
    }


    //更新用户的基本信息
    @RequestMapping("/update/userInfo")
    @RequireToken
    public Object  updateUserInfo(@TokenUserId Long userId, @RequestBody UserInfoParam userInfoParam){
        Result result = mallUserService.updateUserInfo(userInfoParam, userId);

        return  result;
    }


    //上传用户的头像
    @RequestMapping("/update/userImg")
    @RequireToken
    public Object updateUserImg(@TokenUserId Long userId, @RequestParam(name = "file") MultipartFile file){

        //判断文件是否为空
        if(file==null){
            return Result.fail("上传的文件为空");
        }

        String name=file.getOriginalFilename();



        //获取文件
        file.getContentType();



        //获取文件名称 （以用户的id命名比较方便管理) +文件后缀名
        String filename=userId.toString()+name.substring(name.lastIndexOf("."));

        Result result = mallUserService.updateUserImg(file, filename, userId);


        return result;


    }

    //修改用户密码
    @PostMapping("/update/userPassword")
    @RequireToken
    public Object updateUserPassword(@TokenUserId Long userId, @RequestBody UpdatePasswordParam updatePasswordParam){


        Result result = mallUserService.updateUserPassword(updatePasswordParam, userId);

        return result;
    }



}
